package com.project.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory implements InvocationHandler {
    /**目标对象*/
    private Object targetObj;
    public ProxyFactory(Object targetObj){
        this.targetObj = targetObj;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(
                targetObj.getClass().getClassLoader(),
                targetObj.getClass().getInterfaces(),
                this
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("方法执行前");

        //执行目标对象的目标方法，得到方法执行后的返回值
        Object returnObj = method.invoke(targetObj,args);

        System.out.println("方法执行后");
        return returnObj;
    }
}
